﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace DataBase
{
    public partial class Form1 : Form
    {
        Thread th = null;
        Thread th2 = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            th2 = new Thread(new ThreadStart(InitServer));
            th2.Start();
        }
        public void InitServer()
        {
            try
            {
                this.comboBox2.Items.Clear();
                List<ServerInfo> list = AccessDAL.FindServerList();
                foreach (ServerInfo info in list)
                {
                    info.Alias = info.Alias + "（" + info.IP + "）";
                    this.comboBox2.Items.Add(info);
                }
               
                this.comboBox2.DisplayMember = "Alias";
                this.comboBox2.ValueMember = "ID";
                this.comboBox2.SelectedIndex = list.Count - 1;

                ServerInfo server=list.LastOrDefault();
                if (server != null)
                {
                    CommonDAL.conn = string.Format("Data Source={0};Initial Catalog=master;User ID={1};Password={2}", server.IP, server.UserName, server.UserPwd);
                }
            }
            catch (Exception ex)
            {
            }
            
        }
        public void InitDB()
        {
            try
            {
                this.comboBox1.Items.Clear();
                List<string> list = CommonDAL.FindDataBase();
                foreach (string str in list)
                {
                    this.comboBox1.Items.Add(str);
                }
                this.comboBox1.SelectedIndex = list.Count - 1;
            }
            catch (Exception ex)
            {
            }
            th2.Abort();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
            ServerInfo server = (ServerInfo)this.comboBox2.SelectedItem;
            CommonDAL.conn = "Data Source=" + server.IP + ";Initial Catalog=" + this.comboBox1.SelectedItem + ";User ID=" + server.UserName + ";Password=" + server.UserPwd + "";
            th = new Thread(new ThreadStart(exe));
            th.Start();
        }

        private void exe()
        {
            //CommonDAL.conn = "Data Source=42.236.85.97;Initial Catalog=" + this.comboBox1.SelectedItem.ToString() + ";User ID=sa;Password=Gaobo1987";
            
            List<dynamic> list = CommonDAL.FindDataTable();

            string str3 = "";
            string str4 = "";

            //生成接口文件
            string api = "";
            string admin = "";
            api += "using System;\r\n";
            api += "using System.Collections.Generic;\r\n";
            api += "using System.Linq;\r\n";
            api += "using System.Web;\r\n";
            api += "using System.IO;\r\n";
            api += "using Common;\r\n";
            api += "using System.Web.SessionState;\r\n";
            api += "using System.Dynamic;\r\n";
            api += "using Model;\r\n";
            api += "using DAL;\r\n";
            api += "using System.Data;\r\n";
            api += "using System.Dynamic;\r\n";
            api += "using System.Net;\r\n";
            api += "using System.Text;\r\n";
            api += "using System.Web.SessionState;\r\n";
            api += "using JPushAPI;\r\n";
            api += "using System.Security.Cryptography;\r\n";

            api += "namespace UI.tools\r\n";
            api += "{\r\n";
            api += "    /// <summary>\r\n";
            api += "    /// API 的摘要说明\r\n";
            api += "    /// </summary>\r\n";
            api += "    public class Admin : IHttpHandler, IRequiresSessionState\r\n";
            api += "    {\r\n";
            api += "        static Dictionary<string, Func<string>> hs = new Dictionary<string, Func<string>>();\r\n";
            api += "        public void ProcessRequest(HttpContext context)\r\n";
            api += "        {\r\n";
            api += "            context.Response.ContentType = \"text/plain\";\r\n";
            api += "            string action = JobRequest.GetRequestString(\"act\");\r\n";
            api += "            string token = JobRequest.GetRequestString(\"token\");\r\n";
            api += "            string result = string.Empty;\r\n";
            api += "            if (Commons.CheckSign())\r\n";
            api += "            {\r\n";
            api += "                int userid = 0;\r\n";
            api += "                //登录、注册等不检查token\r\n";
            api += "                if (Commons.NoCheckToken.Contains(action))\r\n";
            api += "                {\r\n";
            api += "                    result = hs[action](userid);\r\n";
            api += "                }\r\n";
            api += "                else\r\n";
            api += "                {\r\n";
            api += "                    userid = UserDAL.CheckToken(token);\r\n";
            api += "                    if (userid == 0)\r\n";
            api += "                    {\r\n";
            api += "                        result = Commons.GetFaild(\"身份验证失败\");\r\n";
            api += "                    }\r\n";
            api += "                    else\r\n";
            api += "                    {\r\n";
            api += "                        result = hs[action](userid);\r\n";
            api += "                    }\r\n";
            api += "                }\r\n";
            api += "            }\r\n";
            api += "            else\r\n";
            api += "            {\r\n";
            api += "                result = Commons.GetFaild(\"身份验证失败\");\r\n";
            api += "            }\r\n";
            api += "            context.Response.Write(result);\r\n";
            api += "            context.Response.End();\r\n";
            api += "        }\r\n";
            api += "        static Admin()\r\n";
            api += "        {\r\n";
            foreach (dynamic obj in list)
            {
                string table = obj.tablename;
                string brief = obj.tablebrief == "" ? table : obj.tablebrief;
                string TableName = UpName(table);
                string paras = "";
                string saveinfo = "";
                //生成实体类
                string model = "";
                model += "using System;\r\n";
                model += "using System.Collections.Generic;\r\n";
                model += "using System.Linq;\r\n";
                model += "using System.Text;\r\n\r\n";
                model += "namespace Model\r\n";
                model += "{\r\n";
                model += "    public class " + TableName + "Info\r\n";
                model += "    {\r\n";
                model += "        #region Model\r\n";

                //生成数据类
                string dal = "";
                dal += "using System;\r\n";
                dal += "using System.Collections.Generic;\r\n";
                dal += "using System.Linq;\r\n";
                dal += "using System.Text;\r\n";
                dal += "using System.Data;\r\n";
                dal += "using System.Dynamic;\r\n";
                dal += "using System.Data.SqlClient;\r\n";
                dal += "using System.Configuration;\r\n";
                dal += "using Model;\r\n";
                dal += "using Common;\r\n\r\n";
                dal += "namespace DAL\r\n";
                dal += "{\r\n";
                dal += "    public class " + TableName + "DAL\r\n";
                dal += "    {\r\n";



                string str = "";
                string str2 = "";

                List<dynamic> columns = CommonDAL.FindDataColumn(table);
                str = "            string sql = string.Format(\"insert into [" + table + "](";
                str2 = "            string sql = string.Format(\"update [" + table + "] set ";
                str4 = "";
                foreach (dynamic dy in columns)
                {
                    model += "        private " + Type(dy.type) + " _" + dy.name.ToLower() + ";\r\n";
                    if (dy.name != "ID")
                    {
                        str += "[" + dy.name + "],";
                    }
                }

                str = str.TrimEnd(',') + ") values(";
                str4 += "    info.ID = DataConvert.ToInt(dr[\"ID\"]);\r\n";
                int count = 0;
                foreach (dynamic dy in columns)
                {
                    count++;
                    model += "        /// <summary>\r\n";
                    model += "        /// " + dy.brief + "\r\n";
                    model += "        /// </summary>\r\n";
                    model += "        public " + Type(dy.type) + " " + dy.name + "\r\n";
                    model += "        {\r\n";
                    model += "            set { _" + dy.name.ToLower() + " = value; }\r\n";
                    model += "            get { return _" + dy.name.ToLower() + "; }\r\n";
                    model += "        }\r\n";

                    if (dy.name.ToLower() == "createdate")
                    {
                        saveinfo += "                    " + dy.name + " = DateTime.Now" + (count < columns.Count ? "," : "") + "\r\n";
                    }
                    else
                    {
                        saveinfo += "                    " + dy.name + " = " + dy.name.ToLower() + "" + (count < columns.Count ? "," : "") + "\r\n";
                    }

                    if (dy.name != "ID")
                    {

                        str += "@" + dy.name + ",";
                        if (dy.name != "UserID" && dy.name != "CreateDate")
                        {
                            str2 += "[" + dy.name + "]=@" + dy.name + ",";
                        }

                        if (dy.type == "int")
                        {
                            str4 += "                info." + dy.name + " = DataConvert.ToInt(dr[\"" + dy.name + "\"]);\r\n";
                            if (dy.name.ToLower() != "userid")
                            {
                                paras += "                int " + dy.name.ToLower() + " = JobRequest.GetRequestInt(\"" + dy.name.ToLower() + "\"); //" + dy.brief + "\r\n";
                            }
                        }
                        if (dy.type == "varchar" || dy.type == "text")
                        {
                            str4 += "                info." + dy.name + " = DataConvert.ToString(dr[\"" + dy.name + "\"]);\r\n";
                            paras += "                string " + dy.name.ToLower() + " = JobRequest.GetRequestString(\"" + dy.name.ToLower() + "\"); //" + dy.brief + "\r\n";
                        }
                        if (dy.type == "bit")
                        {
                            str4 += "                info." + dy.name + " = DataConvert.ToBool(dr[\"" + dy.name + "\"]);\r\n";
                            paras += "                bool " + dy.name.ToLower() + " = JobRequest.GetRequestBool(\"" + dy.name.ToLower() + "\"); //" + dy.brief + "\r\n";
                        }
                        if (dy.type == "datetime")
                        {
                            str4 += "                info." + dy.name + " = DataConvert.ToDateTime(dr[\"" + dy.name + "\"]);\r\n";
                            if (dy.name.ToLower() != "createdate")
                            {
                                paras += "                DateTime " + dy.name.ToLower() + " = JobRequest.GetRequestDateTime(\"" + dy.name.ToLower() + "\", DateTime.Now); //" + dy.brief + "\r\n";
                            }
                        }
                        if (dy.type == "decimal")
                        {
                            str4 += "                info." + dy.name + " = DataConvert.ToDouble(dr[\"" + dy.name + "\"]);\r\n";
                            paras += "                double " + dy.name.ToLower() + " = JobRequest.GetRequestDouble(\"" + dy.name.ToLower() + "\"); //" + dy.brief + "\r\n";
                        }
                    }
                    else
                    {
                        paras += "                int " + dy.name.ToLower() + " = JobRequest.GetRequestInt(\"" + dy.name.ToLower() + "\"); //" + dy.brief + "\r\n";
                    }
                }

                str = str.TrimEnd(',') + ");select @@identity;\");\r\n";
                str2 = str2.TrimEnd(',') + " where [ID]=@ID\");\r\n";

                str += "            SqlParameter[] paras = new SqlParameter[]{\r\n";
                str2 += "            SqlParameter[] paras = new SqlParameter[]{\r\n";
                foreach (dynamic dy in columns)
                {
                    if (dy.name != "ID")
                    {
                        str += "                new SqlParameter(\"@" + dy.name + "\", info." + dy.name + "),\r\n";
                        if (dy.name != "CreateDate" && dy.name != "UserID")
                        {
                            str2 += "                new SqlParameter(\"@" + dy.name + "\", info." + dy.name + "),\r\n";
                        }
                    }
                }
                str = str.TrimEnd(",\r\n".ToArray());
                str2 += "                new SqlParameter(\"@ID\", info.ID)\r\n";

                str += "\r\n            };\r\n";
                str2 += "            };\r\n";

                model += "        #endregion Model\r\n";
                model += "    }\r\n";
                model += "}\r\n";

                //保存方法
                dal += "        public static int Save(" + TableName + "Info info)\r\n";
                dal += "        {\r\n";
                dal += str;
                dal += "            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));\r\n";
                dal += "        }\r\n";
                //修改方法
                dal += "        public static int Update(" + TableName + "Info info)\r\n";
                dal += "        {\r\n";
                dal += str2;
                dal += "            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;\r\n";
                dal += "        }\r\n";
                //获取一行数据
                dal += "        public static " + TableName + "Info FindOneByID(int id)\r\n";
                dal += "        {\r\n";
                dal += "            DataRow dr = CommonDAL.FindOneByID(id, \"[" + table + "]\");\r\n";
                dal += "            return SimpleData(dr);\r\n";
                dal += "        }\r\n";
                //获取多行数据
                dal += "        public static List<" + TableName + "Info> FindListByIDs(string ids)\r\n";
                dal += "        {\r\n";
                dal += "            DataTable dt = CommonDAL.FindListByIDs(ids, \"[" + table + "]\");\r\n";
                dal += "            return ListSimpleData(dt);\r\n";
                dal += "        }\r\n";
                //分页
                dal += "        public static List<" + TableName + "Info> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)\r\n";
                dal += "        {\r\n";
                dal += "            StringBuilder filter = new StringBuilder();\r\n";
                dal += "            if (!string.IsNullOrEmpty(keyword))\r\n";
                dal += "            {\r\n";
                dal += "                filter.AppendFormat(\" and [" + table + "].Name like '%{0}%'\", keyword);\r\n";
                dal += "            }\r\n";
                dal += "            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))\r\n";
                dal += "            {\r\n";
                dal += "                filter.AppendFormat(\" and [" + table + "].CreateDate between '{0}' and '{1}'\", DataConvert.ToDateTime(begindate).ToString(\"yyyy-MM-dd 00:00:00\"), DataConvert.ToDateTime(enddate).ToString(\"yyyy-MM-dd 23:59:59\"));\r\n";
                dal += "            }\r\n";
                dal += "            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, \"[" + table + "]\", out count);\r\n";
                dal += "            return ListSimpleData(dt);\r\n";
                dal += "        }\r\n";
                //获取所有
                dal += "        public static List<" + TableName + "Info> FindAll()\r\n";
                dal += "        {\r\n";
                dal += "            DataTable dt = CommonDAL.GetDataTable(string.Format(\"select * from [" + table + "]\"));\r\n";
                dal += "            return ListSimpleData(dt);\r\n";
                dal += "        }\r\n";
                //模型
                dal += "        private static " + TableName + "Info SimpleData(DataRow dr)\r\n";
                dal += "        {\r\n";
                dal += "            " + TableName + "Info info = new " + TableName + "Info();\r\n";
                dal += "            if (dr != null)\r\n";
                dal += "            {\r\n";
                dal += "            " + str4;
                dal += "            }\r\n";
                dal += "            return info;\r\n";
                dal += "        }\r\n";
                //模型
                dal += "        private static " + TableName + "Info AllData(DataRow dr)\r\n";
                dal += "        {\r\n";
                dal += "            " + TableName + "Info info = new " + TableName + "Info();\r\n";
                dal += "            if (dr != null)\r\n";
                dal += "            {\r\n";
                dal += "            " + str4;
                dal += "            }\r\n";
                dal += "            return info;\r\n";
                dal += "        }\r\n";
                //模型
                dal += "        private static List<" + TableName + "Info> ListSimpleData(DataTable dt)\r\n";
                dal += "        {\r\n";
                dal += "            List<" + TableName + "Info> list = new List<" + TableName + "Info>();\r\n";
                dal += "            foreach (DataRow row in dt.Rows)\r\n";
                dal += "            {\r\n";
                dal += "                list.Add(SimpleData(row));\r\n";
                dal += "            }\r\n";
                dal += "            return list;\r\n";
                dal += "        }\r\n";
                //模型
                dal += "        private static List<" + TableName + "Info> ListAllData(DataTable dt)\r\n";
                dal += "        {\r\n";
                dal += "            List<" + TableName + "Info> list = new List<" + TableName + "Info>();\r\n";
                dal += "            foreach (DataRow row in dt.Rows)\r\n";
                dal += "            {\r\n";
                dal += "                list.Add(AllData(row));\r\n";
                dal += "            }\r\n";
                dal += "            return list;\r\n";
                dal += "        }\r\n";
                dal += "    }\r\n";
                dal += "}\r\n";

                api += "            /*" + brief + "*/\r\n";
                api += "            #region 获取" + brief + "\r\n";
                api += "            hs.Add(\"Find_" + TableName + "\", (int userid) =>\r\n";
                api += "            {\r\n";
                api += "                //参数\r\n";
                api += "                int page = JobRequest.GetRequestInt(\"page\"); //第几页\r\n";
                api += "                int limit = JobRequest.GetRequestInt(\"limit\"); //每页条数\r\n";
                api += "                string keyword = JobRequest.GetRequestString(\"keyword\"); //搜索关键词\r\n";
                api += "                string begindate = JobRequest.GetRequestString(\"begindate\"); //开始时间\r\n";
                api += "                string enddate = JobRequest.GetRequestString(\"enddate\"); //结束时间\r\n";
                api += "                int source = JobRequest.GetRequestInt(\"source\"); //来源 1.PC  2.Android  3.IOS\r\n";
                api += "                //参数\r\n";
                api += "                int count = 0;\r\n";
                api += "                List<" + TableName + "Info> list = " + TableName + "DAL.FindPage(keyword, begindate, enddate, page, limit, \"\", \"\", out count);\r\n";
                api += "                return Commons.GetSuccess(\"获取成功\", list, count);\r\n";
                api += "            });\r\n";
                api += "            #endregion\r\n";
                api += "            #region 保存" + brief + "\r\n";
                api += "            hs.Add(\"Save_" + TableName + "\", (int userid) =>\r\n";
                api += "            {\r\n";
                api += "                //参数\r\n";
                api += paras;
                api += "                int source = JobRequest.GetRequestInt(\"source\"); //来源 1.PC  2.Android  3.IOS\r\n";
                api += "                //参数\r\n";
                api += "                int ID = 0;\r\n";
                api += "                " + TableName + "Info info = new " + TableName + "Info()\r\n";
                api += "                {\r\n";
                api += saveinfo;
                api += "                };\r\n";
                api += "                if (id == 0)\r\n";
                api += "                {\r\n";
                api += "                    ID = " + TableName + "DAL.Save(info);\r\n";
                api += "                }\r\n";
                api += "                else\r\n";
                api += "                {\r\n";
                api += "                    ID = " + TableName + "DAL.Update(info);\r\n";
                api += "                }\r\n";
                api += "                if (ID > 0)\r\n";
                api += "                {\r\n";
                api += "                    return Commons.GetSuccess(\"保存成功\", ID);\r\n";
                api += "                }\r\n";
                api += "                else\r\n";
                api += "                {\r\n";
                api += "                    return Commons.GetFaild(\"保存成功\");\r\n";
                api += "                }\r\n";
                api += "            });\r\n";
                api += "            #endregion\r\n";
                api += "            #region 删除" + brief + "\r\n";
                api += "            hs.Add(\"Delete_" + TableName + "\", (int userid) =>\r\n";
                api += "            {\r\n";
                api += "                //参数\r\n";
                api += "                string ids = JobRequest.GetRequestString(\"ids\"); //删除的id集合,中间用,号隔开\r\n";
                api += "                int source = JobRequest.GetRequestInt(\"source\"); //来源 1.PC  2.Android  3.IOS\r\n";
                api += "                //参数\r\n";
                api += "                bool ok = CommonDAL.DeleteByIDs(ids, \"[" + table + "]\");\r\n";
                api += "                if (ok)\r\n";
                api += "                {\r\n";
                api += "                    return Commons.GetSuccess(\"删除成功\", \"\");\r\n";
                api += "                }\r\n";
                api += "                else\r\n";
                api += "                {\r\n";
                api += "                    return Commons.GetFaild(\"删除失败\");\r\n";
                api += "                }\r\n";
                api += "            });\r\n";
                api += "            #endregion\r\n\r\n";

                LogModel(model, TableName + "Info.cs");
                LogDAL(dal, TableName + "DAL.cs");

            }
            api += "        }\r\n";
            api += "        static bool CheckSign()\r\n";
            api += "        {\r\n";
            api += "            try\r\n";
            api += "            {\r\n";
            api += "                StringBuilder sb = new StringBuilder();\r\n";
            api += "                string action = JobRequest.GetRequestString(\"act\");\r\n";
            api += "                string sign = JobRequest.GetRequestString(\"sign\");\r\n";
            api += "                sb.AppendFormat(\"account={0}&act={1}&key={2}\", \"Common_CLIENT\", action, \"365E6B86CE4E43A552G563078D5ADEFD\");\r\n";
            api += "                string signstr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sb.ToString(), \"MD5\").ToLower();\r\n";
            api += "                return sign == signstr;\r\n";
            api += "            }\r\n";
            api += "            catch (Exception ex)\r\n";
            api += "            {\r\n";
            api += "                return false;\r\n";
            api += "            }\r\n";
            api += "        }\r\n";
            api += "        public bool IsReusable\r\n";
            api += "        {\r\n";
            api += "            get\r\n";
            api += "            {\r\n";
            api += "                return false;\r\n";
            api += "            }\r\n";
            api += "        }\r\n";
            api += "    }\r\n";
            api += "}\r\n";
            LogAPI(api);
            //LogAPI(api.Replace("int page = JobRequest.GetRequestInt(\"page\")", "int pageindex = JobRequest.GetRequestInt(\"pageindex\")").Replace("                int limit = JobRequest.GetRequestInt(\"limit\"); //每页条数\r\n", "").Replace("page, limit", "pageindex").Replace("out count);\r\n", "out count);\r\n                dynamic obj = new ExpandoObject();\r\n                obj.list = list;\r\n                obj.count = count;\r\n").Replace("return Commons.GetPager(\"获取成功\", list, count);\r\n", "return Commons.GetSuccess(\"获取成功\", obj);\r\n"));
            this.button1.Enabled = true;
            MessageBoxButtons messButton = MessageBoxButtons.YesNo;
            DialogResult dr = MessageBox.Show("生成成功，是否打开文件夹？", "提示", messButton);
            if (dr == DialogResult.Yes)//如果点击“确定”按钮
            {
                System.Diagnostics.Process.Start(@""+this.textBox1.Text+"/Code");
            }
            th.Abort();
        }

        public void LogModel(string str,string name)
        {
            string folder = this.textBox1.Text;
            string path = folder + "/Code/Model/" + name;
            if (!System.IO.Directory.Exists(folder + "/Code/Model/"))
            {
                System.IO.Directory.CreateDirectory(folder + "/Code/Model/");
            }
            if (System.IO.File.Exists(path))
            {
                File.Delete(path);
            }
            if (!System.IO.File.Exists(path))
            {
                System.IO.FileStream f = System.IO.File.Create(path);
                f.Close();
                f.Dispose();
            }
            
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(path, true);//true为追加,false为覆盖
            System.Text.Encoding.GetEncoding("gb2312");
            f2.WriteLine(str);
            f2.Close();
            f2.Dispose();
        }
        public void LogDAL(string str, string name)
        {
            string folder = this.textBox1.Text;
            string path = folder + "/Code/DAL/" + name;
            if (!System.IO.Directory.Exists(folder+"/Code/DAL/"))
            {
                System.IO.Directory.CreateDirectory(folder + "/Code/DAL/");
            }
            if (System.IO.File.Exists(path))
            {
                File.Delete(path);
            }
            if (!System.IO.File.Exists(path))
            {
                System.IO.FileStream f = System.IO.File.Create(path);
                f.Close();
                f.Dispose();
            }

            System.IO.StreamWriter f2 = new System.IO.StreamWriter(path, true);//true为追加,false为覆盖
            System.Text.Encoding.GetEncoding("gb2312");
            f2.WriteLine(str);
            f2.Close();
            f2.Dispose();
        }
        public void LogAPI(string str)
        {
            string folder = this.textBox1.Text;
            string path = folder + "/Code/API.cs";
            if (!System.IO.Directory.Exists(folder + "/Code/"))
            {
                System.IO.Directory.CreateDirectory(folder + "/Code/");
            }
            if (System.IO.File.Exists(path))
            {
                File.Delete(path);
            }
            if (!System.IO.File.Exists(path))
            {
                System.IO.FileStream f = System.IO.File.Create(path);
                f.Close();
                f.Dispose();
            }

            System.IO.StreamWriter f2 = new System.IO.StreamWriter(path, true);//true为追加,false为覆盖
            System.Text.Encoding.GetEncoding("gb2312");
            f2.WriteLine(str);
            f2.Close();
            f2.Dispose();
        }
        public void LogAdmin(string str)
        {
            string folder = this.textBox1.Text;
            string path = folder + "/Code/Admin.cs";
            if (!System.IO.Directory.Exists(folder + "/Code/"))
            {
                System.IO.Directory.CreateDirectory(folder + "/Code/");
            }
            if (System.IO.File.Exists(path))
            {
                File.Delete(path);
            }
            if (!System.IO.File.Exists(path))
            {
                System.IO.FileStream f = System.IO.File.Create(path);
                f.Close();
                f.Dispose();
            }

            System.IO.StreamWriter f2 = new System.IO.StreamWriter(path, true);//true为追加,false为覆盖
            System.Text.Encoding.GetEncoding("gb2312");
            f2.WriteLine(str);
            f2.Close();
            f2.Dispose();
        }
        public static string UpName(string table)
        {
            return table.Substring(0, 1).ToUpper() + table.Substring(1, table.Length - 1);
        }
        public static string Type(string type)
        {
            if (type == "int" || type == "tinyint" || type == "smallint" || type == "bigint")
            {
                return "int";
            }
            if (type == "varchar" || type == "text" || type == "char")
            {
                return "string";
            }
            if (type == "bit")
            {
                return "bool";
            }
            if (type == "decimal" || type == "money")
            {
                return "double";
            }
            if (type == "datetime" || type == "date")
            {
                return "DateTime";
            }
            return "string";
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.ShowDialog();
            this.textBox1.Text = this.folderBrowserDialog1.SelectedPath;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ServerList serverlist = new ServerList();
            serverlist.ShowDialog();
            th2 = new Thread(new ThreadStart(InitServer));
            th2.Start();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ServerInfo server = (ServerInfo)this.comboBox2.SelectedItem;
            if (server.ID != 0)
            {
                CommonDAL.conn = string.Format("Data Source={0};Initial Catalog=master;User ID={1};Password={2}", server.IP, server.UserName, server.UserPwd);
                th2 = new Thread(new ThreadStart(InitDB));
                th2.Start();
            }
        }
    }
}
